const Post = require('../model/post')
const Links = require('../model/links')

class ContentController {
  async getPostList (ctx) {
    const body = ctx.query
    const sort = body.sort || 'created'
    const page = body.page ? parseInt(body.page) : 0
    const limit = body.limit ? parseInt(body.limit) : 20
    const options = {}
    if (typeof body.catalog !== 'undefined' && body.catalog !== '') {
      options.catalog = body.catalog
    }
    if (typeof body.isTop !== 'undefined') {
      options.isTop = body.isTop
    }
    if (typeof body.status !== 'undefined' && body.status !== '') {
      options.status = body.status
    }
    if (typeof body.isEnd !== 'undefined') {
      options.isEnd = body.isEnd
    }
    if (typeof body.tag !== 'undefined' && body.tag !== '') {
      options.tags = { $elemMatch: { name: body.tag } }
    }
    const result = await Post.getList(options, sort, page, limit)
    ctx.body = {
      code: 200,
      data: result,
      msg: '获取文章列表成功'
    }
  }

  // 查询温馨提醒
  async getTips (ctx) {
    const result = await Links.getList('tips')
    if (result) {
      ctx.body = {
        code: 200,
        data: result
      }
    }
  }

  // 查询友情链接
  async getLinks (ctx) {
    const result = await Links.getList('links')
    if (result) {
      ctx.body = {
        code: 200,
        data: result
      }
    }
  }

  // 本周热议
  async getTopWeek (ctx) {
    const result = await Post.getTopWeek()
    ctx.body = {
      code: 200,
      data: result
    }
  }
}

module.exports = new ContentController()
